/*
** file : data_lnk.h
** date : 02/26/04
*/
#ifndef DATA_LNK_H_
#define DATA_LNK_H_

/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////

#ifdef __cplusplus
extern "C" {
#endif
//
//  single link list structs. it is assumed the first
//  sizeof(void*) bytes are used as pointer to the
//  next element

void *nxtlink_add(void *elm, void *anchor);

void *nxtlink_att(void *elm, void *anchor);

void *nxtlink_del(void *elm, void *anchor);

void* nxtlink_get(void *key, int ksz, int koff, void *anchor);

void* nxtlink_nxt(void *item, void *anchor);


#ifdef __cplusplus
}
#endif

/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////



//
// all double link list element is assume to include
// the following structure dll_t on the top so that
// it can be cast into type of dll_t
// 
typedef struct dll_t {

    struct dll_t  *next;

    struct dll_t  *prev;

}  dll_t;

//
// a double-linked list must have an anchor that is
// of type dll_t, in which 'prev' points to the
// first item in the list amd 'next' points to the
// lost item in the list
//
typedef struct dlh_t {

    struct dll_t  *head;

    struct dll_t  *tail;

    struct dll_t  *hot;    // for application

    long           length; // number of items in list

}  dlh_t;

#ifdef __cplusplus

extern "C" {

inline dll_t* dll_get_next(dll_t* item)
{
    return (item ? item->next : 0);
}

inline dll_t* dll_get_prev(dll_t* item)
{
    return (item ? item->prev : 0);
}

#else 

#define dll_get_prev(item) ((item) ? (item)->prev : 0)
#define dll_get_next(item) ((item) ? (item)->next : 0)

#endif


void dll_add_tail(dll_t* item, dlh_t* anchor);

void dll_add_head(dll_t* item, dlh_t* anchor);

void dll_del_item(dll_t* item, dlh_t* anchor);

// serach dll by comparing a key to a block in dll item
dll_t* dll_key_search(void *key, int size, int offset, dlh_t* anchor);

void dll_sync(dlh_t* anchor);


//
void* talloc(long siz);
void tfree(void *p);

#ifdef __cplusplus
}
#endif




#endif // DATA_LNK_H_
